# Relational Operations

So far we have seen how to create variables and apply arithmetic operations to variables. In programming logic is a very important concept. We want to be able to compare numbers and do something based on that comparison.

A logic statement usually returns as true or false.

True or False?

The earth is flat.

The above statement in logic is False.


C++ has true and false as part of the language. false is defined as 0. So a variable that is equal to 0 in C++ will be equivalent to false in a logic statement. true is defined as NOT false. Any number other than 0 would be interpreted as true in a C++ logic statement.

# Comparison Operators

We will start with a simple integer declaration

int var = 2;

The following are logic operators in C++:

Operator Explanation Example
== equal to var == 3 returns false
!= not equal to var != 3 returns true
< less than var < 3 returns true
> greater than var > 3 returns false
>= greater than or equal var >= 2 returns true
<= less than or equal var <= 3 returns true

# If Statement

We want to be able to check a logic condition and then do something based on that comparison. An If condition simply checks if our logic statement is true and it executes some code that we write. And if the statement is false, it will execute a different part of our code.

if( LOGIC STATEMENT)
{
    // code to run if the statement is true
}
else
{
    // code to run if the statement is false
}

The above shows how we can use an if condition in C++. Recall what we explored with the Modulo Operator. It can be used to check if a number is even or odd.

Exercise

The following example checks the modulo 2 of the integer variable number and prints to the Serial if the number is even or odd.

Change the value of number and rerun the program to observe the output.

NOTE

  • At the end of if( ) we do not put a semicolon.

  • In the program we used Serial.print instead of Serial.println. The difference is that the print will not print a newline at the end of the characters. This is useful for this case because we are printing the number and then the text on the same line. If we want to print a newline manually, we can write in our string \n

    Try it out yourself. Modify the print line to the following:

    Serial.print("\ni\ns\ne\nv\ne\nn");
    

# If else

In some cases we want to check another condition if the first one returns false. For example, let's say we want to do different specific things if our number is positive, if our number is negative or if our number is equal to 0.

The same syntax of the If Statement would be used but we will have to add another condition between the if and the else.

if(LOGIC STATEMENT 1)
{
    //if LOGIC STATEMENT 1 is true do this
}
else if(LOGIC STATEMENT 2)
{
    //if LOGIC STATEMENT 1 is false and LOGIC STATEMENT 2 is true 
    //then do this 
}
else
{
    //if LOGIC STATEMENT 1 and LOGIC STATEMENT 2 is false
    //then do this
}


Homework

The program below is missing a logic statement for the if else condition to check if the number is negative. Add the correct comparison for the program to compile and run correctly.


# Logic Operators

We can also combine relational statements. For example, if we have two variables and we want to check if both of them are positive or if we want to check the opposite of a statement.

To combine relational statements we use &&, || and !

# && Operator

This operator is the AND operator. Both statements have to be true for the result to be true as well. If at least one of the statements is false then the result will be false.

int var1 = 2;
int var2 = 10;

if( (var1 > 0) && (var2 > 0) )
{
    //both variables are positive
}
else
{
    //at least one of the variables is 
    //less than or equal to 0
    //we don't know which one.
    //We only know that one of the conditions
    //does not return true
}

# || Operator

This operator is the OR operator. Both statements have to be false for the result to be false as well. If at least one of the statements is true then the result will be true.

int var1 = 2;
int var2 = -10;

if( (var1 > 0) || (var2 > 0) )
{
    //at least one of the variables is 
    //greater than 0
    //we don't know which one, 
    //or if both of them are positive
    //We only know that at least one of the conditions
    //does return true
}
else
{
    //both variables are less than or equal to 0
}

# ! Operator

This operator is different than the not equal (!=) operator. ! is NOT operator. It negates everything after it.

int var1 = 0;
if(!(var1 == 0))
{
  //do this if NOT ( var1 equal to 0)
  //which means if var1 is not equal to 0
}

The ! operator can be combined with any of the above operators in between relational statements.

Pay attention to the parentheses in this example.

if( !((var1 == 0) && (var2 == 0)) )
{
  
}

To understand what this condition is checking for, let's analyze the condition with all possible returns of the inner statements. var1 == 0 can be true if var1 is equal to 0 and false otherwise. Same applies to var2 == 0.

var1 == 0 var2 == 0
false false
false true
true false
true true

The above table defines all the possible combinations that we can have for the two relational statements we have.

We will start by analyzing (var1 == 0) && (var2 == 0)

var1 == 0 var2 == 0 (var1 == 0) && (var2 == 0)
false false false
false true false
true false false
true true true

So now we know, based on all the possible combinations, what the logic will return inside the parentheses. We will simplify our table and replace (var1 == 0) && (var2 == 0) with INNER LOGIC Let's add the final logic operator in our statement to our table and negate everything we evaluated in INNER LOGIC.

var1 == 0 var2 == 0 INNER LOGIC !( INNER LOGIC )
false false false true
false true false true
true false false true
true true true false

Now from this table, we can understand what will be returned from our logic statement. For example if var1 = 1 and var2 = 0, the second row will give us the return of the full statement:

var1 == 0 var2 == 0 INNER LOGIC !( INNER LOGIC )
false true false true

# Multiple Conditions

Homework

The following program uses three variables with an if condition.

If var3 = 1 and we don't know what var1 and var2 are equal to, can you determine if the expression inside the if statement is true or false? explain.

HINT

Build a table starting with the relational statements, and then create a column for every logic statement starting with the inner ones until you get the result of the full statement.

var1 > 0 var2 > 0 var3 == 0
false false false
false false true
false true false
false true true
true false false
true false true
true true false
true true true